package com.haiheng.module_main.fragment

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.viewModels
import com.haiheng.common.constant.AppConstant
import com.haiheng.common.dialog.StandardDialog
import com.haiheng.common.fragment.RouterFragment
import com.haiheng.library_base.dialog.LoadingDialog
import com.haiheng.library_base.model.BaseViewModel
import com.haiheng.library_base.toast.ToastUtils
import com.haiheng.module_main.R
import com.haiheng.module_main.databinding.FragmentDataSynchronizationBinding
import com.haiheng.module_main.mode.AlarmedBooksViewModel
import com.haiheng.module_main.mode.BookInfoViewModel
import com.haiheng.module_main.mode.BookRetirementViewModel
import com.haiheng.module_main.mode.DataSynchronizationViewModel
import kotlin.math.ceil
import kotlin.math.round

/**
 *
 * @Des: 数据同步
 * @Author: hwz
 * @Date:2023/6/17 0017
 * @Version: 1.0
 */
class DataSynchronizationFragment : RouterFragment() {
    private lateinit var binding: FragmentDataSynchronizationBinding
    private val viewModel: DataSynchronizationViewModel by viewModels()
    private lateinit var bookRetirementViewModel: BookRetirementViewModel
    private lateinit var alarmedBooksViewModel: AlarmedBooksViewModel
    private lateinit var bookInfoViewModel: BookInfoViewModel
    private lateinit var loadingDialog : LoadingDialog
    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding = FragmentDataSynchronizationBinding.inflate(inflater, container, false)
        return binding.root
    }

    override fun initView() {
        loadingDialog = LoadingDialog(requireContext(), R.style.AvLoadingDialogStyle)
        bookInfoViewModel = BookInfoViewModel()
        bookRetirementViewModel = BookRetirementViewModel(bookInfoViewModel)
        alarmedBooksViewModel = AlarmedBooksViewModel(bookInfoViewModel)
        binding.includeTile.tvTitle.setText(getText(R.string.text_syn))
        binding.includeTile.cbRignt.visibility = View.GONE
        binding.includeTile.ivBack.setOnClickListener {
            activity.navigateUp()
        }
        binding.includeTile.cbRignt.isChecked = false
        binding.tvSynTime.setText("最近同步时间：\n${AppConstant.getLastSnyTime()}")
        binding.btnSubmit.setOnClickListener {
            viewModel.synBookByPage(1)
        }

    }

    override fun initViewModel(): BaseViewModel? {
        viewModel.totolNumber.observe(viewLifecycleOwner){
            binding.progressBar.max = it
        }
        viewModel.synProgressSize.observe(viewLifecycleOwner){
            binding.progressBar.progress = it
            var progress = it/binding.progressBar.max.toDouble()
            var percentage =  ceil(progress*100)
            binding.tvSynTime.setText("同步中...\n" +
                    "${percentage}%")
            if (percentage == 100.0){
                binding.tvSynTime.setText("同步完成")
            }
        }
        viewModel.showMsg.observe(viewLifecycleOwner){
            loadingDialog.hide()
            ToastUtils.showShort(it)
        }
        viewModel.synBooInfoNumber.observe(viewLifecycleOwner){
            //同步完书籍信息后，同步剔旧和报警清单
            bookRetirementViewModel.queryBookRetirementByNet()
            alarmedBooksViewModel.queryAlarmedBooksByNet()
        }
        alarmedBooksViewModel._alarmedBooksData.observe(viewLifecycleOwner){
            showSnyResult()
        }
        bookRetirementViewModel._bookRetirementData.observe(viewLifecycleOwner){
            showSnyResult()
        }
        viewModel.synFinish.observe(viewLifecycleOwner){
            showSnyResult()
        }
        viewModel.smartRecordList.observe(viewLifecycleOwner){
            if (it.size > 0){
                StandardDialog.newInstance().setTitle("有未上传智慧上架记录")
                    .setContent("上传本地智慧上架记录到服务器")
                    .setOnDialogListener(object : StandardDialog.OnDialogClickListener{
                        override fun onCancel(dialog: StandardDialog) {
                            dialog.dismiss()
                        }
                        override fun onConfirm(dialog: StandardDialog) {
                            viewModel.synSmartRecord()
                            dialog.dismiss()
                        }
                    }).show(childFragmentManager)
            }
        }
        viewModel.recordList.observe(viewLifecycleOwner){
            if (it.size > 0){
                StandardDialog.newInstance().setTitle("有未上传上架记录")
                    .setContent("上传本地同步记录到服务器")
                    .setOnDialogListener(object : StandardDialog.OnDialogClickListener{
                        override fun onCancel(dialog: StandardDialog) {
                            dialog.dismiss()
                        }

                        override fun onConfirm(dialog: StandardDialog) {
                            viewModel.synShelpRecord()
                            dialog.dismiss()
                        }
                    }).show(childFragmentManager)
            }

        }
        viewModel.bookWithdrawalRecordList.observe(viewLifecycleOwner){
            if (it.size > 0){
                StandardDialog.newInstance().setTitle("有未上传下架、剔旧记录")
                    .setContent("上传本地下架、剔旧记录到服务器")
                    .setOnDialogListener(object : StandardDialog.OnDialogClickListener{
                        override fun onCancel(dialog: StandardDialog) {
                            dialog.dismiss()
                        }

                        override fun onConfirm(dialog: StandardDialog) {
                            viewModel.synBookWithdrawal()
                            dialog.dismiss()
                        }
                    }).show(childFragmentManager)
            }
        }
        viewModel.bookShiftingRecordList.observe(viewLifecycleOwner){
            if (it.size > 0){
                StandardDialog.newInstance().setTitle("有未上传倒架记录")
                    .setContent("上传本地倒架记录到服务器")
                    .setOnDialogListener(object : StandardDialog.OnDialogClickListener{
                        override fun onCancel(dialog: StandardDialog) {
                            dialog.dismiss()
                        }

                        override fun onConfirm(dialog: StandardDialog) {
                            viewModel.synBookShif()
                            dialog.dismiss()
                        }
                    }).show(childFragmentManager)
            }
        }
        return viewModel
    }

    fun showSnyResult(){
        binding.tvRecordInfo.setText("同步书籍信息： ${viewModel.synBooInfoNumber.value}条 \n " +
                "同步剔旧清单: ${bookRetirementViewModel._bookRetirementData.value?.size}条 \n"+
                "同步报警清单: ${alarmedBooksViewModel._alarmedBooksData.value?.size}条 \n"+
                "上传上架,剔旧记录: ${viewModel.synRecordNumber.value}条 \n"+
                "上传下架记录: ${viewModel.synWithdrawalRecordNumber.value}条 \n"+
                "上传智慧上架: ${viewModel.synSmartRecordNumber.value}条 \n"
        )
    }
    override fun initLoad() {

    }



}